<?php
class wms_mdl_delivery_items extends dbeav_model{
    public function getDeliveryIdByPbn($product_bn){
        $sql = 'SELECT count(1) as _c FROM sdb_wms_delivery_items WHERE bn like \''.addslashes($product_bn).'%\'';
        $count = $this->db->selectrow($sql);
        if ($count['_c'] >=10000) {
            $offset = 0; $limit = 9000; $list = array();
            $sql = 'SELECT delivery_id FROM sdb_wms_delivery_items WHERE bn like \''.addslashes($product_bn).'%\'';
            $total = floor($count['_c']/$limit);
            for ($i=$total;$i>=0;$i--) {
                $rows = $this->db->selectlimit($sql,$limit,$i*$limit);
                if ($rows) {
                    $list = array_merge_recursive($list,$rows);
                }
            }
            return $list;
        }
        $sql = 'SELECT delivery_id FROM sdb_wms_delivery_items WHERE bn like \''.addslashes($product_bn).'%\'';
        $rows = $this->db->select($sql);
        return $rows;
    }

    public function getBn($product_bn,$status)
    {
        $whereSql="ods.bn = '".addslashes($product_bn)."'";
       	if(is_array($status)){
	 if(count($status)>1){
            $whereSql.=' and  od.status in ('.implode(',', $status).')';
        }else{
	   $whereSql.=' and  od.status='.$status[0];
	}
	}else{
	      $whereSql.=' and  od.status='.$status;
	    error_log(var_export(array($whereSql),true),3,PUBLIC_DIR."/lakers20-29.log");

	}   
        $sql = "SELECT COUNT(od.delivery_Id) as _c  FROM sdb_wms_delivery od
                LEFT JOIN sdb_wms_delivery_items ods ON od.delivery_id = ods.delivery_id
                WHERE ".$whereSql;
        $count = $this->db->selectrow($sql);
        $sql = "SELECT od.delivery_id
                 FROM sdb_wms_delivery od
                LEFT JOIN sdb_wms_delivery_items ods ON od.delivery_id = ods.delivery_id
                WHERE ".$whereSql;
    
        if ($count['_c'] >=10000) {
            $offset = 0; $limit = 9000; $list = array();
            $total = floor($count['_c']/$limit);
            for ($i=$total;$i>=0;$i--) {
                $rows = $this->db->selectlimit($sql,$limit,$i*$limit);
                if ($rows) {
                    $list = array_merge_recursive($list,$rows);
                }   
            }   
            return $list;
        }   
        $rows = $this->db->select($sql);
       if(base_request::get_remote_addr()=='101.229.86.227')
        {
            error_log(var_export(array($sql,$rows,$status),true),3,PUBLIC_DIR."/lakers20-29.log");
        }   
        return $rows;
    }

    
    //按条形码搜索
    public function getDeliveryIdByPbarcode($product_barcode){
        $sql = 'SELECT count(1) as _c FROM sdb_wms_delivery_items as I LEFT JOIN '.
                'sdb_material_codebase as c ON I.product_id=c.bm_id WHERE c.code like \''.addslashes($product_barcode).'%\'';
        $count = $this->db->selectrow($sql);
        if ($count['_c'] >= 10000) {
            $offset = 0; $limit = 9000; $list = array();
            $sql = 'SELECT delivery_id FROM sdb_wms_delivery_items as I LEFT JOIN '.
                    'sdb_material_codebase as c ON I.product_id=c.bm_id WHERE c.code like \''.addslashes($product_barcode).'%\'';
            $total = floor($count['_c']/$limit);
            for ($i=$total;$i>=0;$i--) {
                $rows = $this->db->selectlimit($sql,$limit,$i*$limit);
                if ($rows) {
                    $list = array_merge_recursive($list,$rows);
                }
            }
            return $list;
        }
        
        $sql = 'SELECT delivery_id FROM sdb_wms_delivery_items as I LEFT JOIN '.
                'sdb_material_codebase as c ON I.product_id=c.bm_id WHERE c.code like \''.addslashes($product_barcode).'%\'';
        $rows = $this->db->select($sql);
        
        return $rows;
    }
}
